<?php
/**
 * Description of goods
 *
 * @author YeFng
 */
 
namespace c\goods;
\control::$template_dir = TEMPLATE_DIR_A;

class a extends \control
{
    var $table = 'goods';
    var $auth_m = 'goods';
    var $State = array('hide','publish');   
    var $class = array('PU'=>'\c\auth\permission');
    
    function __construct() {
        parent::__construct();
        
        $this->FM = \YF_f::initC('\c\file\map', '', 'goods_pic');
        $this->SM = \YF_f::initC('\c\goods\sort\map');
        
        $this->AttrC = \YF_f::initC('\c\goods\attr\a');
        $this->title = \lang::g('Goods');
    }
    
    function index_home()
    {
        $this->PU->check($this->auth_m);
        $this->loadTemplate('goods/home');
    }
    // list
    function index_list_a()
    {
        $this->PU->check($this->auth_m);    
        
        $s = $this->P_G('s');
        $where = '';
        $field = '*';
        $tables = array($this->table =>'');
        if ($s['name'])
         {
            $n = $this->db->escape($s['name']);
            $where .= " AND( title LIKE '%$n%' or description LIKE '%$n%' or barcode LIKE '$n')";
         }
        if ($s['sort_id'])
         {
            $tables['sm'] = array('as'=>'goods_sort_map','join'=>'LEFT JOIN','on'=>'sm.data_id=goods.id');
            if ($s['sort_id']=='null')
                $where .= ' AND ISNULL(sm.sort_id)';
            else{
                $s_id = intval($s['sort_id']);
                $where .= " AND sm.sort_id IN($s_id)";
                $tables['sm']['join'] = 'RIGHT JOIN';
                $field .=',data_id id';
             }
         }
        if ($s['state']!='')
            $where .= ' AND state='. intval($s['state']);
        if ($s['type_id']!='')
            $where .= ' AND type_id='. intval($s['type_id']);
        
        $order = $_GET['LO'];
        if ($order['col'])
         {
            $way = ($order['way']=='ASC' ? ' ASC' : ' DESC');
            $_A = array('id', 'hits', 'create_time', 'title');
            $order = in_array($order['col'], $_A) ? $order['col'] . $way :'';
         }
        else
            $order = 'goods.id DESC';
       
        
        $where && $where = substr($where, 4);
        $res = $this->db->get_page(array(), $tables, $field, $where, $order);
        $this->loadTemplate('goods/list_a', array('res'=>$res, 'state'=>\lang::Ag($this->State)));
        $html = ob_get_clean();
        
        ajax_json(array('status'=>TRUE, 'res'=>$html,
            'paging'=>$this->db->paging,
            'u_get'=>\url::p('&p&limit&LO&s[sort_id]'),
             ));
    }
    // map
    function index_sort()
    {
        $this->PU->check($this->auth_m);
        $pid = intval($_GET['id']);
        $res = $this->SM->tree($pid);
        ajax_json(array('status'=>TRUE, 'res'=>$res));
    }
    
    /**
     * 编辑
     */
    function index_post()
    {
        $this->PU->check($this->auth_m);
        $id = intval($_GET['id']);
        $type_R = $this->t_var['type_R'] = \YF_f::initC('\c\attribute\type_g')->getLi();
    	if ($id)
    	 {
            $this->title = \lang::g('Edit') .$this->title;
    	 	$data = $this->db->getOne($this->table, "*", 'id='.$id);
            $this->isExist($data);

            //files
            $this->t_var['file_R'] = $this->FM->get($id);
            $this->t_var['sort_R'] = $this->SM->get($id);
    	 }
    	else{
            $this->title = \lang::g('Add') .$this->title;
            $data = array( 'hits'=>0, 'state'=>1, 'num'=>1000 );

            if (is_array($_GET['data'])){
                $data = array_merge($data, $_GET['data']);
             }
    	 }
        $this->t_var['data'] = $data;
        $this->loadTemplate('goods/post');
        $this->dispLayout = 'sub';
    }
    function index_save()
    {
        $this->PU->check($this->auth_m);
        $p = $_POST;
        $id = intval($p['data_id']);
        $data = $p['data'];
        $content = $p['data_content'];

        if ($id)
		 {
			$where = "id=$id";
            $data['modified_time']= date('YmdHis');
		 }
		else{
			$where = '';
            if (!$data['keyword'])
                $data['keyword'] = \YF_f::initC('keyword')->getKeys( $content . $data['description'] . $data['title']);
			if (!$data['description'] && $content)
				$data['description'] = mb_substr(trim(strip_tags($content)), 0, 80);
            if (!$data['title'] && $data['description'])
                $data['title'] = mb_substr(strip_tags($data['description']), 0, 10);
            if (!$data['pic'] && $p['fileA'])
                $data['pic'] = $p['fileA']['a'][0];
            
            $data['money'] +=0;
            $data['money_old'] +=0;
            $data['freight_money'] +=0;
            $data['content'] = '';
		 }
        if ($content)
         {
            $content = preg_strip_tags($content);
            if ($p['down_pic'])
                    $content = \YF_f::initC ('\c\img\remote')->content_match($content);
            $data['content'] = $content;
         }
         
        isset($data['type_id']) && $data['type_id'] +=0;
        isset($data['company_id']) && $data['company_id'] +=0;
        
        $b = $this->db->save($this->table, $data, $where);
		if ($b)
         {
			if ($id){
				\log::write($this->table, 'edit', $id);
             }
			else {
				$id = $this->db->insert_id();
				\log::write($this->table, 'add', $id);
			 }
            $msg = '';
            
            // attr
            $b = $where ? $this->AttrC->editData($id) : $this->AttrC->save($id);
            if (!$b)
                $msg .= lang::g('Save attr over.');
            
            // sort
            if ($p['sortA_del'])
                $this->SM->del($id, intval_ids($p['sortA_del']));
            if (is_array($p['sortA_add']))
                $this->SM->save($id, array_unique($p['sortA_add']));
  
            //file
            if ($p['fileA'])
             {
                $fA = $p['fileA'];
                if ($fA['d'])
                    if (!$this->FM->del($id, $fA['d']))
                        $msg .= lang::g('Del file over.');
                if ($fA['a'])
                    if (!$this->FM->add($id, $fA['a']))
                        $msg .= lang::g('Save file over.');
             }
             
			ajax_json(array('status'=>TRUE, 'id'=>$id, 'msg'=>$msg));
		 }
        ajax_json(\lang::g('save fail')); 
    }
    function index_del()
    {
        $this->PU->check($this->auth_m);
        $ids = intval_ids($_GET['id']);
       
        if ($this->clear($ids))
         {
            $this->db->del($this->table, "id IN($ids)");
            if ($this->db->num_affected()>0)
             {
                \log::write($this->table, 'del', $ids);
                ajax_json(TRUE);	
             }            
         }

		ajax_json(\lang::g( 'Over'));
    }
    function clear($id)
    {
        $this->FM->del($id);
        $this->SM->del($id);
        $this->AttrC->del($id);
        return true;
    }
    
    function index_removeSort()
    {
        $this->PU->check($this->auth_m);
        $id = intval_ids($_GET['id']);
        $this->SM->del($id, intval($_GET['sort_id']));
        ajax_json(TRUE);
    }
    function index_addSort()
    {
        $this->PU->check($this->auth_m);
        $ids = intval_ids($_GET['id']);
        $a = array(intval($_GET['sort_id']));
        foreach (explode(',', $ids) as $id)
            $this->SM->save($id, $a);
        ajax_json(TRUE); 
    }
    
}
